<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SynchData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:synchData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->synch();
        return 0;
    }

    //同步数据
    private function synch()
    {
        $max_issue = DB::table('history_win')->max('issue') ?? 0;
        $list = DB::table('history_data')->select('issue','red_no')->where('issue','>',$max_issue)->orderBy('id','asc')->get();
        $data = [];
        foreach ($list as $k => $v){
            $a = intval(substr($v->red_no,0,2));
            $b = intval(substr($v->red_no,2,2));
            $c = intval(substr($v->red_no,4,2));
            $d = intval(substr($v->red_no,6,2));
            $e = intval(substr($v->red_no,8,2));
            $f = intval(substr($v->red_no,10,2));
            $data[] = [
                'red_one'   => $a,
                'red_two'   => $b,
                'red_three' => $c,
                'red_four'  => $d,
                'red_five'  => $e,
                'red_six'   => $f,
                'odd_num'   => $a%2 + $b%2 + $c%2 + $d%2 + $e%2 + $f%2,
                'red_no'    => $v->red_no,
                'issue'     => $v->issue,
                'max_cont'  => maxCont([$a,$b,$c,$d,$e,$f]),
            ];
            Db::table('gather')->where('red_no',$v->red_no)->update(['win_num'=>1]);
        }

        Db::table('history_win')->insert($data);
    }
}
